package com.zyd.work.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zyd.common.AjaxUtils;
import com.zyd.common.CommonController;
import com.zyd.work.domain.vo.DayLog;
import com.zyd.work.logic.IDayLogFacade;
import com.zyd.xtgl.domain.vo.TbAdmUser;

@Controller
@RequestMapping("/daylog")
public class DayLogAction extends CommonController {
	private IDayLogFacade dayLogImpl;

	@Resource(name = "dayLogImpl")
	public void setDayLogImpl(IDayLogFacade dayLogImpl) {
		this.dayLogImpl = dayLogImpl;
	}

	@RequestMapping(params = "method=getAllList")
	public ModelAndView getAllList() {
		return new ModelAndView();
	}

	@RequestMapping(params = "method=addDayLog")
	public ModelAndView addDayLog(HttpServletRequest request,
			HttpServletResponse response, @Valid DayLog worklog,BindingResult result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if (result.hasErrors()) {
			out.write("输入有误,添加日志失败~~~~");
		}else{
			TbAdmUser user = (TbAdmUser)request.getSession().getAttribute("user");
			worklog.setUserid(user.getUserid());
			dayLogImpl.addDayLog(worklog);
			out.write("添加日志成功~~~~");
		}
		out.close();
		return null;
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @param worklog
	 * @param result
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateDayLog")
	public ModelAndView updateDayLog(HttpServletRequest request,
			HttpServletResponse response, @Valid DayLog worklog,BindingResult result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if (result.hasErrors()) {
			out.write("输入有误,更新日志失败~~~~");
		}else{
			dayLogImpl.updateDayLog(worklog);
			out.write("更新日志成功~~~~");
		}
		out.close();
		return null;
	}
	/**
	 * 转向到日志首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=toDayLog")
	public ModelAndView toDayLog(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("worklog/mydaylog");
	}
	/**
	 * 根据登录用户获取当天日志的列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getNowDateDayLogList")
	public String getNowDateDayLogList(HttpServletRequest request,
			HttpServletResponse response) {
		TbAdmUser user = (TbAdmUser)request.getSession().getAttribute("user");
		String date = request.getParameter("nowdate");
		List<DayLog> list =	dayLogImpl.getListByDate(date,user.getUserid());
		return AjaxUtils.ajaxList(request, response, list);
	}
	/**
	 * 根据传入ID获取日志详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getDayLogById")
	public String getDayLogById(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		DayLog worklog =	dayLogImpl.getDayLogById(id);
		return AjaxUtils.ajaxList(request, response, worklog);
	}
	/**
	 * 此方法，用于个人日志查询
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=searchDayLogList")
	public String searchDayLogList(HttpServletRequest request,
			HttpServletResponse response) {
		TbAdmUser user = (TbAdmUser)request.getSession().getAttribute("user");
		String startdate = request.getParameter("startdate");
		String enddate = request.getParameter("enddate");
		List<DayLog> list =	dayLogImpl.searchDaylog(startdate, enddate,user.getUserid());
		return AjaxUtils.ajaxList(request, response, list);
	}
	/**
	 * 此方法是专门的日志提交方法
	 * 查询出的日志如果没有提交可以传入提交状态，用此方法更新
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=commitDayLog")
	public ModelAndView commitDayLog(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String iscommit= request.getParameter("iscommit");
		DayLog daylog = dayLogImpl.getDayLogById(id);
		daylog.setIscommit(iscommit);
		dayLogImpl.updateDayLog(daylog);
		out.write("提交成功~~~~");
		out.close();
		return null;
	}
}
